package malus.shaofeng.com.beiwomusic.utils;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by aaa on 15-3-15.
 */
public class Player implements MediaPlayer.OnBufferingUpdateListener,
        MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {

    public MediaPlayer mediaPlayer;
    private SeekBar seekBar;
    private Timer timer=new Timer();
    private TextView already_time;
    private TextView remain_time;

    public Player(SeekBar seekBar, TextView already_time, TextView remain_time){
        super();
        this.seekBar=seekBar;
        this.already_time=already_time;
        this.remain_time=remain_time;
        mediaPlayer=new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnCompletionListener(this);

        timer.schedule(timerTask,0,1000);
    }

    TimerTask timerTask=new TimerTask() {
        @Override
        public void run() {
            if (mediaPlayer==null){
                return;
            }
            if (mediaPlayer.isPlaying()&&seekBar.isPressed()==false){
                   handler.sendEmptyMessage(0);
            }
        }
    };

    Handler handler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int currentPosition = mediaPlayer.getCurrentPosition();
            int duration = mediaPlayer.getDuration();
            int remained = duration - currentPosition;

            if (duration>0){
                float pos = seekBar.getMax() * currentPosition / (duration*1.0f);
                seekBar.setProgress((int)pos);
                String current = PlayTimeUtil.calcTimeLenght(currentPosition);
                String remain = PlayTimeUtil.calcTimeLenght(remained);
                already_time.setText(current);
                remain_time.setText(remain);
            }
        }
    };

    public boolean isPlayed(){
        if (mediaPlayer.isPlaying()){
            return true;
        }
        else {
            return false;
        }
    }

    public void playUrl(String url){
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void play(){
        mediaPlayer.start();
    }

    public void pause(){
        mediaPlayer.pause();
    }

    public void stop(){
        if (mediaPlayer!=null){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer=null;
        }
    }


    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        seekBar.setSecondaryProgress(percent);
        int currentProgress = seekBar.getMax()
                * mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration();
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {

    }
}
